<template>
  <div class="header" :class="scroll ? 'active' : ''">
    <van-sticky>
      <div class="header-goBack" @click="onBack">
        <img class="backButton" src="../../../img/backButton.png" />
      </div>
      <template v-if="!allPhotosList">
        <div class="header-title" v-show="scroll">
          {{ data.film?.name }}
        </div>
      </template>
      <template v-else>
        <div class="header-title" v-show="scroll">
          剧照({{ data.film?.photos.length }})
        </div>
      </template>
    </van-sticky>
  </div>
  <div
    class="info"
    @scroll="scrollEvent"
    :class="allPhotosList ? 'active' : ''"
  >
    <div class="poster">
      <div class="poster-logo">
        <img src="../../../img/maizuo_logo.png" />
      </div>
      <van-image width="100%" :src="data.film?.poster" class="poster-img">
        <template v-slot:loading>
          <div class="loading">
            <van-loading color="white" size="60" />
          </div>
        </template>
      </van-image>
    </div>
    <div class="filmInfo" v-if="data.film">
      <div class="filmInfoBox1">

        <div class="p1">
          <div>
            <span class="name">{{ data.film?.name }}</span>
            <span class="filmType">{{ data.film?.filmType.name }}</span>
          </div>
          <div>
            <span class="grade">{{ data.film?.grade }}</span>
            <span class="gradeText" v-show="data.film?.grade">分</span>
          </div>
        </div>
        <div class="p2">
          <span class="category">{{ data.film?.category }}</span>
        </div>
        <div class="p3">
          <span class="premiereAt">{{ premiereAt }}</span>
        </div>
        <div class="p4">
          <span class="nation">{{ data.film?.nation }}</span> |
          <span class="runtime">{{ data.film?.runtime }}分钟</span>
        </div>
      </div>
      <div class="filmInfoBox2">
        <div class="synopsis" :class="{ hidden: ishidden }">
          <div class="synopsis-text">{{ data.film?.synopsis }}</div>
        </div>
        <div class="hiddenSynopsis" @click="changeHidden">
          <van-icon name="arrow-down" v-if="ishidden" />
          <van-icon name="arrow-up" v-else />
        </div>
      </div>
    </div>
    <div class="actorsInfo" v-if="data.film">
      <div class="actorsTitle">演职人员</div>
      <div class="actorsLIstDiv">
        <ul>
          <li v-for="item in data.film?.actors" class="actorsList">
            <img
              :src="item.avatarAddress"
              @click="seachActor(item.name)"
              class="actorsImg"
            />
            <span class="name">{{ item.name }}</span>
            <span class="role">{{ item.role }}</span>
          </li>
        </ul>
      </div>
    </div>
    <div class="filmPhotos" v-if="data.film">
      <div class="filmPhotosTitle">
        剧照
        <div class="allPhotos" @click="goToAllPhotosList">
          全部({{ data.film?.photos.length }})
          <van-icon name="arrow" />
        </div>
      </div>
      <div class="filmPhotoList">
        <div class="filmPhotoDiv" v-for="item in data.film?.photos">
          <!-- <van-image-preview v-model:show="show" :images="images"> -->
          <img :src="item" @click="clickImg(item)" />
          <!-- </van-image-preview> -->
        </div>
      </div>
    </div>
    <div class="ticket" @click="buyTicket">选座购票</div>
  </div>
  <div v-if="allPhotosList" class="allPhotosListDiv">
    <div class="photoDiv" v-for="item in data.film?.photos">
      <img :src="item" />
    </div>
  </div>
</template>

<script>
export default {
  name: "film",
};
</script>
<script setup>
import { onMounted, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getFilmInfo } from "../../api/filmInfo";
import dayjs from "dayjs";

const data = ref({});
const route = useRoute();
const router = useRouter();
const id = route.params.id;
const premiereAt = ref();
const scroll = ref(false);
const allPhotosList = ref(false);
const ishidden = ref(true);
const show = ref(false);
const images = ref([]);
// const VanImagePreview = ImagePreview.Component; //图片预览

onMounted(() => {
  getFilmInfo(id)
    .then((response) => {
      // data.value = JSON.parse(JSON.stringify(response.data));
      data.value = response.data;
      console.log(data.value);
      premiereAt.value = dayjs(response.data.film.premiereAt * 1000).format(
        "YYYY-MM-DD上映"
      );
    })
    .catch((error) => {
      alert("数据错误");
    });
});
function scrollEvent(event) {
  //页面滚动 判断吸顶
  const scrollTop = event.target.scrollTop;
  if (scrollTop >= 44) {
    scroll.value = true;
  } else {
    scroll.value = false;
  }
}

function onBack() {
  //回退按钮
  if (allPhotosList.value) {
    allPhotosList.value = false;
  } else {
    router.back();
  }
}
function goToAllPhotosList() {
  allPhotosList.value = true;
}
function buyTicket() {
  router.push(`/film/${id}/cinemas`);
  console.log(id);
}
function changeHidden() {
  ishidden.value = !ishidden.value;
}
function clickImg(img) {
  images.value = [];
  images.value.push(img);
}
function seachActor(name) {
  const searchUrl = `https://www.baidu.com/s?wd=${encodeURIComponent(name)}`;
  window.open(searchUrl, "_blank");
}
</script>

<style scoped lang="scss">
.info {
  height: 790px;
  overflow: auto;
  background-color: #f4f4f4;
}
.info.active {
  display: none;
}
.header {
  position: fixed;
  width: 100%;
  height: 44px;
  z-index: 1;
  &-goBack {
    height: 30px;
    position: absolute;
    top: 5px;
    left: 5px;
    img {
      width: 30px;
    }
  }

  &-title {
    font-size: 17px;
    line-height: 44px;
    width: 100%;
    text-align: center;
  }
}
.header.active {
  background-color: white;
  color: #000;
}
.poster {
  width: 100%;
  height: 210px;
  overflow: hidden;
  position: relative;
  &-logo {
    display: flex;
    justify-content: center;
    height: 210px;
  }
  &-img {
    overflow: hidden;
    position: absolute;
    top: -80%;
  }
}
.filmInfo {
  // height: 190px;
  padding: 15px;
  font-size: 13px;
  color: #797d82;
  background-color: white;

  .filmInfoBox1 {
    line-height: 24px;
    .p1 {
      display: flex;
      position: relative;
      justify-content: space-between;

      .name {
        color: #191a1b;
        font-size: 18px;
        height: 24px;
        line-height: 24px;
        margin-right: 7px;
      }
      .filmType {
        font-size: 9px;
        color: #fff;
        background-color: #d2d6dc;
        height: 14px;
        line-height: 14px;
        padding: 0 2px;
        display: inline-block;
        border-radius: 2px;
        position: absolute;
        top: 20%;
      }
      .grade {
        font-size: 18px;
        font-style: italic;
        color: #ffb232;
      }
      .gradeText {
        font-size: 10px;
        color: #ffb232;
      }
    }
  }
  .filmInfoBox2 {
    margin-top: 12px;
    // height: 90px !important;
    // overflow: hidden;
    line-height: 18px;
    position: relative;

    .synopsis {
      height: 200px;
      // margin-top: 12px;
      letter-spacing: 0.8px; //文字间隔
      overflow: hidden;
      transition: height 0.5s ease; //动画
      -webkit-transition: height 0.5s ease;
      -o-transition: height 0.5s ease;
      transition: height 0.5s ease;
    }
    .hidden {
      height: 35px !important;
      // overflow: hidden;
    }
    .hiddenSynopsis {
      text-align: center;
      line-height: normal;
      width: 20px;
      margin: auto;
    }
  }
}
.loading {
  top: 45%;
  left: 40%;
  padding: 15px;
  position: fixed;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.7);
  z-index: 999;
}
.actorsInfo {
  // height: 190px;
  width: 100%;
  margin: 15px 0;
  // display: flex;
  background-color: white;
  padding-top: 15px;
  overflow: hidden;

  .actorsTitle {
    padding-left: 15px;
    margin-bottom: 15px;
  }
  .actorsLIstDiv {
    ul {
      width: 100%;
      overflow: auto;
      display: flex;
      .actorsList {
        width: 85px;
        min-width: 85px;
        margin-left: 10px;
        .actorsImg {
          width: 100%;
        }
        .name,
        .role {
          display: block;
          width: 100%;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .name {
          padding-top: 5px;
          font-size: 12px;
          color: #191a1b;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .role {
          font-size: 10px;
          color: #797d82;
        }
      }
    }
  }
}
.filmPhotos {
  height: 190px;
  width: 100%;
  background-color: white;
  padding-top: 15;
  margin-bottom: 171px;
  padding-top: 15px;
  .filmPhotosTitle {
    margin: 15px;
    .allPhotos {
      float: right;
      font-size: 13px;
      color: #797d82;
    }
  }
  .filmPhotoList {
    height: 100px;
    width: 100%;
    // position: relative;
    overflow: auto;
    display: flex;
    .filmPhotoDiv {
      img {
        margin-left: 15px;
        height: 100px;
        // position: absolute;
        // top: 50%;
      }
    }
  }
}
.allPhotosListDiv {
  margin-bottom: 1.5px;
  margin-right: 1.5px;
  margin-top: 44px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  .photoDiv {
    width: 123px;
    height: 123px;
    overflow: hidden;
    background-image: url(../../../img/backImg.png);
    background-size: 80px;
    background-position: 50%;
    background-repeat: no-repeat;
    margin-right: 1px;
    img {
      width: 200px;
      display: block;
    }
  }
}

.ticket {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 49px;
  width: 100%;
  text-align: center;
  background-color: #ff5f16;
  color: #fff;
  font-size: 16px;
  line-height: 49px;
}
</style>
